﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Net;
using CommonClasses;

namespace ProxyManager
{
    class ProxyMain
    {

        private static int _page = 3;
        private static string _patternSait;
        private static string _patternIP;
        private static MatchCollection _matchCol;
        private static Regex _regex;
        private static WebClient client;
        private static CountdownEvent _countdown;


        static void Main(string[] args)
        {
            //  id="serp_res_(\d+)">\s*<div class="res-wrap">\s*<h3 class="res-head">\s*<a\s*[\S, ]*\s*target="[\w]*"\s*href="http://(?:www.)?([-\w.]+/[\S]*)"
            _patternSait = @"id=""serp_res_(\d+)"">\s*<div class=""res-wrap"">\s*<h3 class=""res-head"">\s*<a\s*[\S, ]*\s*target=""[\w]*""\s*href=""http://(?:www.)?([-\w.]+/[\S]*)""";
            //  (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{2,5})
            _patternIP = @"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{2,5})";

            Thread th1 = new Thread(SiteGrabber);  th1.Start();
            Thread th2 = new Thread(GrabberIP);    th2.Start();

            Thread th4 = new Thread(ReGrabber);    th4.Start();

            DataProviderProxy _db = new DataProviderProxy();
            while (true)
            {
                List<string> listIP = _db.GetUncheckedIP(100);
                if (listIP != null)
                {
                    _countdown = new CountdownEvent(listIP.Count);
                    foreach (var item in listIP)
                    {
                        new Thread(Checker).Start(item);
                    }
                    _countdown.Wait();
                }
                Thread.Sleep(50);
            }
        }


        public static void SiteGrabber()
        {
            while (true)
            {
                using (client = new WebClient())
                {
                    DataProviderProxy _db = new DataProviderProxy();
                    client.Encoding = Encoding.UTF8;
                    string resault = client.DownloadString(@"http://go.mail.ru/search?q=+"":8080"" +"":3128"" +"":80""&num=10&rch=e&sf=" + _page * 10);
                    _regex = new Regex(_patternSait);
                    _matchCol = _regex.Matches(resault);
                    foreach (Match match in _matchCol)
                    {
                        Console.WriteLine("найден: " + match.Groups[2].Value);
                        _db.InsertSite(match.Groups[2].Value);
                    }
                    _page++;
                }
                Thread.Sleep(1000 * 60 * 3);
            }
        }

        public static void GrabberIP()
        {
            while (true)
            {
                Thread.Sleep(1000 * 20);
                using (client = new WebClient())
                {
                    DataProviderProxy _db = new DataProviderProxy();
                    client.Encoding = Encoding.UTF8;
                    Dictionary<int, string> dbResault = _db.GetSiteWithID();
                    if (dbResault == null) continue;
                    try
                    {
                        string resault = client.DownloadString(@"http://" + dbResault.Values.ElementAt(0));
                        _regex = new Regex(_patternIP);
                        _matchCol = _regex.Matches(resault);
                        foreach (Match match in _matchCol)
                        {
                            Console.WriteLine("найден ip: " + match.Groups[1].Value);
                            _db.InsertIP(dbResault.Keys.ElementAt(0), match.Groups[1].Value);
                        }
                    }
                    catch { Console.WriteLine("errorConnect 404"); }
                    finally { _db.UpdateDate(dbResault.Keys.ElementAt(0), DateTime.Now); }
                }
            }
        }

        public static void Checker(object stat)
        {
            WebRequest webReq = WebRequest.Create(@"http://bitfry.narod.ru/02.htm");
            DataProviderProxy _db = new DataProviderProxy();
            string ip = stat.ToString();
            try
            {
                webReq.Proxy = new WebProxy(ip);
                webReq.Timeout = 3000;
                WebResponse webRes = webReq.GetResponse();
                Console.WriteLine("отличная прокся: " + ip);
                _db.UpdateStatus(ip, StatusIP.Good, "ip");
            }
            catch
            {
                _db.DeleteIP(ip);
                Console.WriteLine("плохая прокся del: " + ip);
            }
            finally { _countdown.Signal(); }
        }

        public static void ReGrabber()
        {
            while (true)
            {
                Thread.Sleep(1000 * 60 * 60);
                DataProviderProxy _db = new DataProviderProxy();
                Dictionary<int, string> resault = _db.GetOldSites();
                if (resault == null) continue;
                foreach (var item in resault)
                {
                    _db.UpdateDate(item.Key, null);
                    _db.UpdateStatus(item.Key.ToString(), StatusIP.Unchecked, "idSite");
                }
            }
        }


    }

}
